package adapters;

import android.content.Context;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.example.administrator.newxlmedical.R;

import java.text.Collator;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.Locale;

import beans.OperateGoodsDetailsBean;
import butterknife.BindView;
import custom.BaseRecyclerAdapter;

/**
 * Created by Administrator on 2018/5/22.
 */

public class InventoryErrorAdapter extends BaseRecyclerAdapter<InventoryErrorAdapter.InventoryErrorViewHolder> {

    private Context mContext;
    private LayoutInflater inflater;
    private List<OperateGoodsDetailsBean.DataBean> list;
    private OnClickListener onClickListener;

    public InventoryErrorAdapter(Context mContext, List<OperateGoodsDetailsBean.DataBean> list) {
        this.mContext = mContext;
        this.list = list;
        inflater = LayoutInflater.from(mContext);
    }

    @Override
    public InventoryErrorViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        InventoryErrorViewHolder viewHolder = new InventoryErrorViewHolder(inflater.inflate(R.layout.adapter_inventory_error, parent, false));
        return viewHolder;
    }
    public void addData(List<OperateGoodsDetailsBean.DataBean> list,int type ){
        if (list!=null){
            this.list.clear();
            this.list.addAll(list);
            sort(type);
            notifyDataSetChanged();
        }
    }

    private void sort(int type) {
        if (type == 1) { // 按物品名排序
            Collections.sort(list, new Comparator<OperateGoodsDetailsBean.DataBean>() {
                @Override
                public int compare(OperateGoodsDetailsBean.DataBean o1, OperateGoodsDetailsBean.DataBean o2) {
                    Collator ca = Collator.getInstance(Locale.CHINA);
                    if (ca.compare(o2.getGoodName(), o1.getGoodName()) < 0) {
                        return 1;
                    } else if (ca.compare(o2.getGoodName(), o1.getGoodName()) == 0) {
                        return 0;
                    } else {
                        return -1;
                    }
                }
            });
        } else if (type == 2) { // 按供应商排序
            Collections.sort(list, new Comparator<OperateGoodsDetailsBean.DataBean>() {
                @Override
                public int compare(OperateGoodsDetailsBean.DataBean o1, OperateGoodsDetailsBean.DataBean o2) {
                    Collator ca = Collator.getInstance(Locale.CHINA);
                    if (ca.compare(o2.getManufacturer(), o1.getManufacturer()) < 0) {
                        return 1;
                    } else if (ca.compare(o2.getManufacturer(), o1.getManufacturer()) == 0) {
                        return 0;
                    } else {
                        return -1;
                    }
                }
            });
        }
//        adapter = new CountAdapter(CountActivity.this, sortList);
//        recyclerView.setAdapter(adapter);
    }

    @Override
    public void onBindViewHolder(final InventoryErrorViewHolder viewHolder, final int position) {
        OperateGoodsDetailsBean.DataBean bean = list.get(position);
//        viewHolder.tvCabinetNumber.setText(String.valueOf(bean.getSelectCabinet()));
        viewHolder.tvName.setText(bean.getGoodName());
        viewHolder.tvSpecification.setText(bean.getSpecification());
        viewHolder.tvSupplier.setText(bean.getManufacturer());
        viewHolder.tvCount.setText(String.valueOf(bean.getNumber()));
        viewHolder.tvUnit.setText(bean.getGoodsUnit());
        if (bean.getStatus() == 1) {
            viewHolder.tvState.setText( "已匹配");
        } else if (bean.getStatus() == 2) {
            viewHolder.tvState.setText( "已验收");
        } else if (bean.getStatus() == 3) {
            viewHolder.tvState.setText( "已放入");
        } else if (bean.getStatus() == 4) {
            viewHolder.tvState.setText( "已取出");
        } else if (bean.getStatus() == 5) {
            viewHolder.tvState.setText( "已使用");
        } else if (bean.getStatus() == 6) {
            viewHolder.tvState.setText( "已退货");
        }
        if (position % 2 == 0) {
            viewHolder.linearLayout.setBackgroundColor(mContext.getResources().getColor(R.color.partLineBackground));
        } else {
            viewHolder.linearLayout.setBackgroundColor(mContext.getResources().getColor(R.color.white));
        }

        viewHolder.tvState.setTextColor(Color.RED);

//        viewHolder.tvSpecification.setOnLongClickListener(new View.OnLongClickListener() {
//            @Override
//            public boolean onLongClick(View v) {
//                onClickListener.onShowDetail(position,viewHolder.tvSpecification,410);
//                return true;
//            }
//        });
//
//        viewHolder.tvName.setOnLongClickListener(new View.OnLongClickListener() {
//            @Override
//            public boolean onLongClick(View v) {
//                onClickListener.onShowDetail(position,viewHolder.tvName,250);
//                return true;
//            }
//        });
//
//        viewHolder.tvSupplier.setOnLongClickListener(new View.OnLongClickListener() {
//            @Override
//            public boolean onLongClick(View v) {
//                onClickListener.onShowDetail(position,viewHolder.tvSupplier,640);
//                return true;
//            }
//        });
    }

    @Override
    public int getItemCount() {
        return list.size();
    }

    class InventoryErrorViewHolder extends BaseRecyclerAdapter.BaseViewHolder {
        @BindView(R.id.tv_adapter_inventory_error_name)
        TextView tvName;
        @BindView(R.id.tv_adapter_inventory_error_specification)
        TextView tvSpecification;
        @BindView(R.id.tv_adapter_inventory_error_supplier)
        TextView tvSupplier;
        @BindView(R.id.tv_adapter_inventory_error_count)
        TextView tvCount;
        @BindView(R.id.tv_adapter_inventory_error_unit)
        TextView tvUnit;
        @BindView(R.id.tv_adapter_inventory_error_state)
        TextView tvState;
        @BindView(R.id.ll_inventory_error_title)
        LinearLayout linearLayout;
        public InventoryErrorViewHolder(View itemView) {
            super(itemView);
        }
    }

    public void setOnClickListener(OnClickListener onClickListener) {
        this.onClickListener = onClickListener;
    }

    public interface OnClickListener {
        //        void onShowDetailSpecification(int position, TextView view);
        void onShowDetail(int position, TextView view,int xOffWidth);
//        void onShowDetailSupplier(int position, TextView view);
    }
}
